package com.loubii.sunshineSchool.ui.fragments;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.loubii.sunshineSchool.R;
import com.loubii.sunshineSchool.adapter.CenterAdapter;
import com.loubii.sunshineSchool.app.MainApplication;
import com.loubii.sunshineSchool.httpRequest.HttpBin;
import com.loubii.sunshineSchool.util.ImageUtil;

import java.io.IOException;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

import static android.app.Activity.RESULT_OK;

public class FragmentCenter extends BaseFragment {
    @BindView(R.id.rv_mine)
    RecyclerView mRvMine;
    @BindView(R.id.headview)
    ImageView headView;


    public FragmentCenter() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void initView(View view) {
        //图片
        ImageUtil.downloadAvatar("http://192.168.43.130:2021/static/image/user" + (((MainApplication) context.getApplication()).getUid()) % 10 + ".jpg", headView);

        headView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, null);
                intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        "image/*");
                startActivityForResult(intent, 0x1);
            }
        });

        initRecycleView();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {        //此处的 RESULT_OK 是系统自定义得一个常量
            return;
        }
        Bitmap bm = null;
        //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口
        ContentResolver resolver = context.getContentResolver();
        //此处的用于判断接收的Activity是不是你想要的那个
        if (requestCode == 0x1) {
            try {
                Uri originalUri = data.getData();        //获得图片的uri
                bm = MediaStore.Images.Media.getBitmap(resolver, originalUri);        //显得到bitmap图片

                String[] proj = {MediaStore.Images.Media.DATA};
                //好像是android多媒体数据库的封装接口，具体的看Android文档
                Cursor cursor = context.managedQuery(originalUri, proj, null, null, null);
                //按我个人理解 这个是获得用户选择的图片的索引值
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                //将光标移至开头 ，这个很重要，不小心很容易引起越界
                cursor.moveToFirst();
                //最后根据索引值获取图片路径
                String path = cursor.getString(column_index);
                HttpBin.uploadImage(context, path, new HttpBin.ResponseListener<Boolean>() {
                    @Override
                    public void onResponse(Call<Boolean> call, Response<Boolean> response) {

                    }

                    @Override
                    public void onFailure(Call<Boolean> call, Throwable t) {

                    }
                });
            } catch (IOException e) {

            }
        }
    }

    private void initRecycleView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRvMine.setLayoutManager(linearLayoutManager);
        mRvMine.setAdapter(new CenterAdapter(context));
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_center;
    }


}
